#!/usr/bin/env python


#import html5lib
#from html5lib import treebuilders
from BeautifulSoup import BeautifulSoup, BeautifulStoneSoup
import sys
import re
import urllib2
ru = lambda x: (x,'cp1251')

out = open("python_out.html", "w")
sys.stdout = out

url = "http://go.mail.ru/search?mailru=1&drch=e&q=macintosh+%EF%F0%EE%E4%E0%E6%E0+%EF%E5%F0%EC%FC&rch=e"

#coding = 'Unicode'
#coding = 'Windows-1251'
#coding = 'CP-866'
#coding = 'ISO-IR-111'
#coding = 'ISO-8859-5'
coding = 'UTF-8'
#coding = 'CP-1251'
#coding = 'ASCII'

page = urllib2.urlopen(url)
soup = BeautifulSoup(page, fromEncoding=coding)
#parser = html5lib.HTMLParser(tree=treebuilders.getTreeBuilder("beautifulsoup"))
#soup = parser.parse(urllib2.urlopen(url).read())

s=""
s1=""
i=0

print '<html>\n <head>\n  <title>GParser</title>\n  <meta http-equiv="Content-Type" content="text/html;charset=utf-8">\n </head>\n <body>\n\n'
for li_tag in soup.findAll(['li']):
	for div_tag in li_tag.findAll(['div']):
		if (str(div_tag)[:22] == '<div class="res-wrap">'):
			n=0
			for p_tag in div_tag.findAll(['a', 'p']):
				n=n+1
				if n<3:
					s = (str(p_tag))
					s1 = ""
					for i in range(len(s)):
						if (s[i]!="\t"):
							if (s[i]!="\n"):
								s1 = s1 + s[i]
					if s1[1]=='p':
						s1=s1[16:-4]
					print s1+"\n<br />"
			print "<br />\n"

print
for div_tag in soup.findAll(['div']):
	if (str(div_tag)[:20] == '<div class="ad-itm">'):
		print div_tag.h3.a.prettify()
		print "<br />\n"+str(div_tag.p)[17:-4]+"\n<br />"
		#print div_tag.span.a.prettify() + "\n<br />"
		print "<br />\n"
#		s = (str(div_tag))
#		s1 = ""
#		for i in range(len(s)):
#			if (s[i]!="\t"):
#				if (s[i]!="\n"):
#					s1 = s1 + s[i]
#				else:
#					if (s1[len(s1)-1]!="\n"):
#						s1 = s1 + s[i]
#		print s1
#		print
print '\n\n </body>\n</html>'

out.close()
